package console

import (
	"duoke/service"
	"duoke/tables"
	"duoke/utils/orm"
	"duoke/utils/response"
	"github.com/gin-gonic/gin"
	"time"
)

var Dashboard = new(dashboardCtl)

type dashboardCtl struct{}

func (ctl *dashboardCtl) Index(c *gin.Context) {
	accountR, _ := service.Request.Account(c)
	accountNum, _ := orm.Db.Where("id >? AND role =?", 1, "account").Count(new(tables.Account))
	expireAccount := make([]tables.Account, 0)
	staffNum, _ := orm.Db.Where("acid =?", accountR.Id).Count(new(tables.AccountStaff))
	if accountR.Id == 1 {
		_ = orm.Db.Where("id <>?  AND role =?", 1, "account").
			Where("expire_time >?", time.Now().Unix()-86400*15).
			Cols("id", "username", "nickname", "phone", "avatar", "name", "expire_time", "last_login_time", "last_login_ip").
			Find(&expireAccount)
	}
	data := gin.H{
		"account_num":    accountNum,
		"expire_account": expireAccount,
		"staff_num":      staffNum,
	}
	response.Success(c, "", data)
}
